// pkg_shop/pages/salesRatio/salesRatio.js
import request from '../../../utils/http.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    dianPuId: 0,
    dianPuLevel:1,//店铺等级
    dianPuLevelStr:'',


    ztRatio: 0,
    ztDisable:false,
    jtRatio: 0,
    jtDisable:false,
    ygRatio: 0,
    ygDisable:true,
    syMax: 100, //店铺剩余佣金比例

    yongHuYongJinDengJi:1,



  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      dianPuId:options.dianPuId,
    })
    this.getRatio();
    // this.getCustonLevel();
  },

  //获取佣金比例
  getRatio: function () {
    var that = this
    let data = {
      dianPuId: that.data.dianPuId,
    }

    request.get('dianPu/getYongJinBiLv', data, res => {

      console.log("获取店铺数据:" + JSON.stringify(res.data))
      
      that.setData({
        // ztRatio: that.data.dianPuLevel==3||that.data.dianPuLevel==6?0:res.data.zhiJieShangJiBiLv,
        // ztDisable:that.data.dianPuLevel==3||that.data.dianPuLevel==6?true:false,
        // jtRatio:(that.data.dianPuLevel==1||that.data.dianPuLevel==4)?res.data.jianJieShangJiBiLv:0,
        // jtDisable:(that.data.dianPuLevel==1||that.data.dianPuLevel==4)?false:true,
        ygRatio:res.data.dianPuYuanGongBiLv,
        ygDisable:false

      })

      that.setData({
        syMax: 100 - that.data.ygRatio
      })

      


    }, err => {
      wx.showToast({
        title: '查询失败', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })
  },

    //获取客户设置等级
  getCustonLevel: function () {
      var that = this
      let data = {
        dianPuId: that.data.dianPuId,
      }
  
      request.get('dianPu/basicInfo', data, res => {
        that.setData({
          yongHuYongJinDengJi:res.data.yongHuYongJinDengJi ,
        })

      }, err => {
        wx.showToast({
          title: '查询失败', // 提示的内容
          icon: "none", // 图标，默认success
          duration: 1000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })
      })
    },

  //修改佣金比例
  updateRatio: function () {

    wx.showLoading({
        title: '保存中...',
        mask: true
    })

    var that = this
    let data = {
      dianPuId: that.data.dianPuId,
      zhiJieShangJiBiLv: that.data.ztRatio,
      jianJieShangJiBiLv: that.data.jtRatio,
      dianPuYuanGongBiLv: that.data.ygRatio,
    }

    request.get('dianPu/setYongJinBiLv', data, res => {
      var mess=res.data.message
      wx.hideLoading()
      wx.showToast({
        title: mess, // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })

      
      if(res.data.status==1){
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 1000)
      }

    }, err => {
      wx.showToast({
        title: '查询失败', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })
  },

  //修改客户等级
  radioChange(e) {
   // console.log('客户等级：', e.detail.value)
    this.setData({
      yongHuYongJinDengJi:e.detail.value
    })

    var that = this
    let data = {
      dianPuId: that.data.dianPuId,
      leiXing:12,
      ziDuan:that.data.yongHuYongJinDengJi
    }
    request.get('dianPu/setDianPuMingZi', data, res => {
     
      if(res.data.status==1){

      }else{
        wx.showToast({
          title: '修改失败', // 提示的内容
          icon: "none", // 图标，默认success
          duration: 1000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })
      }
    }, err => {
      wx.showToast({
        title: '修改失败，请联系技术人员', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },


  ztchange: function (e) {
    var vleData = e.detail.value
    var that = this
    var tempData = 100 - (that.data.jtRatio + that.data.ygRatio + vleData)
    that.setData({
      ztRatio: vleData
    })
    if (tempData < 0) {
      tempData = tempData + that.data.jtRatio
      that.setData({
        jtRatio: tempData > 0 ? tempData : 0
      })
      if (tempData < 0) {
        tempData = tempData + that.data.ygRatio
        that.setData({
          ygRatio: tempData > 0 ? tempData : 0
        })
      }
    }
    that.setData({
      syMax: 100 - that.data.ztRatio - that.data.jtRatio - that.data.ygRatio
    })
  },

  jtchange: function (e) {
    var vleData = e.detail.value
    var that = this
    var tempData = 100 - (that.data.ztRatio + that.data.ygRatio + vleData)

    that.setData({
      jtRatio: vleData
    })


    if (tempData < 0) {
      tempData = tempData + that.data.ygRatio
      that.setData({
        ygRatio: tempData > 0 ? tempData : 0
      })
      if (tempData < 0) {
        tempData = tempData + that.data.ztRatio
        that.setData({
          ztRatio: tempData > 0 ? tempData : 0
        })
      }
    }
    that.setData({
      syMax: 100 - that.data.ztRatio - that.data.jtRatio - that.data.ygRatio
    })
  },

  ygchange: function (e) {
    var vleData = e.detail.value
    var that = this
    var tempData = 100 - (that.data.ztRatio + that.data.jtRatio + vleData)

    that.setData({
      ygRatio: vleData
    })
    if (tempData < 0) {
      tempData = tempData + that.data.ztRatio
      that.setData({
        ztRatio: tempData > 0 ? tempData : 0
      })
      if (tempData < 0) {
        tempData = tempData + that.data.jtRatio
        that.setData({
          jtRatio: tempData > 0 ? tempData : 0
        })
      }
    }
    that.setData({
      syMax: 100 - that.data.ztRatio - that.data.jtRatio - that.data.ygRatio
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})